<?php
/**
  * CLOUD
  *
  * This is not a freeware, use is subject to license terms
  * @author CLOUD DEV TEAM
  * 
  */
class Common
{
	/**
	 * 开始session
	 */
	/*public static function loadSession()
	{
		$systemConfig = self::getConfig( 'System' );
		if ( $systemConfig['session_storage'] == 'mysql' )
		{
			return Session_SessionMysql::getInstance();
		}
		else
		{
			return Session_SessionFile::getInstance();
		}
	}*/
	/**
	 * 获取配置信息
	 * @param string $file	配置文件名
	 * @param string $key	配置数组索引
	 * @param bool $reload	是否强制重新载入
	 */
	public static function getConfig( $file , $key = '' , $reload = false )
	{
		static $config = array();
		$file = ucfirst( $file );
		if ( !$reload && isset( $config[$file] ) )
		{
			if ( empty( $key ) )
			{
				return $config[$file];
			}
			elseif ( isset( $config[$file][$key] ) )
			{
				return $config[$file][$key];
			}
		}
		$path = DATA . 'Config/' . $file . '.php';
		if ( file_exists( ROOT_DIR . $path ) )
		{
			$config[$file] = include ROOT_DIR . $path;
			if ( empty( $key ) ) 
			{
				return $config[$file];
			} 
			elseif ( isset( $config[$file][$key] ) )
			{
				return $config[$file][$key];
			}
		}
		else 
		{
			exit( '<div style="font-size:14px;text-align:left; border:1px solid #9cc9e0; padding:3px 4px;color:#000000;font-family:Arial, Helvetica,sans-serif;"><b>Error url : </b>' . $_SERVER['REQUEST_URI'] . '<br /><b> Error Message : </b> Config file "' . $file .  '.php" lose! <br /></div>' );
		}
		return;
	}
	
	/**
	 * 写配置文件
	 * @param string $file
	 * @param mix $value
	 */
	public static function writeConfig( $file , $value )
	{
		$path = DATA . 'Config/' . ucfirst( $file ) . '.php';
		$configFile = ROOT_DIR . $path;
		$data = "<?php\r\nreturn " . var_export( $value , true ) . ";\r\n?>";
		$filesize = file_put_contents( $configFile , $data , LOCK_EX );
		
		if ( $filesize )
		{
			return true;
		}
		return false;
	}
	
	/**
	 * 获取数据库实例
	 */
	public static function getDb()
	{
		return Db_Mysql::getInstance();
	}
	
	/**
	 * 引用字符串
	 * @param mix $str
	 */
	public static function deep_addslashes( $str )
	{
		if( is_array( $str ) )
		{
			foreach( $str as $key=>$val )
			{
				$str[$key] = self::deep_addslashes( $val );
			}
		}
		else
		{
			$str = addslashes( trim( $str ) );
		}
		return $str;
	}
	
	/**
	 * 反引用一个引用字符串
	 * @param mix $str
	 */
	public static function deep_stripslashes( $str )
	{
	 	if( is_array( $str ) )
	 	{
	 		foreach( $str as $key => $val )
	 		{
	 			$str[$key] = self::deep_stripslashes( $val );
	 		}
	 	}
	 	else
	 	{
	 		$str = stripslashes( $str );
	 	}
	 	return $str;
	}
	
	/**
	 * 实体化
	 * @param mix $str
	 */
	public static function deep_htmlspecialchars( $str )
	{
	 	if( is_array( $str ) )
	 	{
			foreach( $str as $key => $val )
			{
				$str[$key] =  self::deep_htmlspecialchars( $val );
			}
		}
		else
		{
			$str = htmlspecialchars( $str );
		}
		return $str;
	}
	
	/**
	 * 合并两个数组
	 * @param array $arr1
	 * @param array $arr2
	 */
	public static function deep_merge( $arr1 , $arr2 )
	{
		if ( $arr2 && is_array( $arr2 ) )
		{
			foreach( $arr2 as $k => $v )
			{
				if ( !isset( $arr1[$k]) || !is_array( $v ) ) 
				{
					$arr1[$k] = $v;
				}
				else 
				{
					$arr1[$k] = self::deep_merge( $arr1[$k] , $v );
				}
			}
		}
		return $arr1;
	}
	
	/**
	 * 输出错误信息
	 * @param int $code
	 * @param string $msg
	 */
	public static function errorOutput( $msg )
	{
		exit( '<div style="font-size:14px;text-align:left; border:1px solid #9cc9e0; padding:3px 4px;color:#000000;font-family:Arial, Helvetica,sans-serif;"><b>Error Message : </b> ' . $msg . ' <br /></div>' );
	}
	
	/**
	* 将字符串转换为数组
	*
	* @param	string	$data	字符串
	* @return	array	返回数组格式，如果，data为空，则返回空数组
	*/
	public static function string2array( $data )
	{
		$array = array();
		if( empty( $data ) ) 
		{
			return $array;
		}
		eval( "\$array = $data;" );
		return $array;
	}
	/**
	 * 将数组转换为字符串
	 * @param array $data
	 * @param bool $isFormData
	 */
	public static function array2string( $data , $isFormData = true )
	{
		if($data == '')
		{
			return '';
		}
		if( $isFormData ) 
		{
			$data = Common::deep_stripslashes( $data );
		}
		return Common::deep_addslashes( var_export( $data , true ) );
	}
	
	/**
	 * 获取随机字符串
	 * @param int $length
	 * @param string $string
	 */
	public static function getRandNum( $length = '' , $string = '' )
	{
		$length = $length ? $length : 6;
		$string = $string ? $string : 'abcdefghigklmnopqrstuvwxwyABCDEFGHIGKLMNOPQRSTUVWXWY0123456789';
		$hash = '';
		$max = strlen( $string ) - 1;
		for( $i = 0; $i < $length; $i++ ) 
		{
			$hash .= $string[mt_rand( 0 , $max )];
		}
		return $hash;
	}
	
	/**
	 * 获取系统版本信息
	 * @param string $type
	 */
	public static function getVersion( $type = 'version' )
	{
		static $versionInfo = '';
		if ( !file_exists( ROOT_DIR . DATA . 'Config/Version.php' ) )
		{
			Url::showMsg( LANG::getLang( 'Common' , 'version file is not exists!' ) );
		}
		$versionInfo = include_once ROOT_DIR . DATA . 'Config/Version.php';
		return $versionInfo[$type];
	}
	
	/**
	 * 获取上传最大容量限制
	 * @param string $type
	 */
	public static function getUploadMaxSize( $type = 'M' )
	{
		$maxSize = ini_get( 'upload_max_filesize' );
		if ( $type == 'M' )
		{
			if ( strpos( $maxSize , 'M' ) !== false )
			{
				return $maxSize;
			}
			elseif ( strpos( $maxSize , 'K' ) !== false )
			{
				return intval( $maxSize ) / 1024;
			}
		}
		elseif ( $type == "K" )
		{
			if ( strpos( $maxSize , 'M' ) !== false )
			{
				return intval( $maxSize ) * 1024;
			}
			elseif ( strpos( $maxSize , 'K' ) !== false )
			{
				return intval( $maxSize );
			}
		}
	}
}